package com.genntii.examManager.controller;

import com.genntii.examManager.common.constant.CommonConstant;
import com.genntii.examManager.common.result.PageResult;
import com.genntii.examManager.common.result.Result;
import com.genntii.examManager.domain.dto.CourseDTO;
import com.genntii.examManager.domain.dto.CourseUpdateDTO;
import com.genntii.examManager.domain.entity.Subject;
import com.genntii.examManager.domain.vo.CourseVO;
import com.genntii.examManager.domain.vo.UserVO;
import com.genntii.examManager.service.CourseRecordService;
import com.genntii.examManager.service.CourseService;
import com.genntii.examManager.service.SubjectService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course/t")
public class Course4tController {

    @Resource
    private CourseRecordService courseRecordService;

    @Resource
    private CourseService courseService;

    @Resource
    private SubjectService subjectService;

    @GetMapping("/student/{courseId}")
    public Result<PageResult<UserVO>> getStudentListByCourse(@Param("pageNum") Integer pageNum,
                                                             @Param("pageSize") Integer pageSize,
                                                             @RequestParam(value = "sortField", required = false) String sortField,
                                                             @RequestParam(value = "sortOrder", required = false) String sortOrder,
                                                             @PathVariable Long courseId) {
        return Result.success(courseRecordService.getStudentListByCourse(pageNum, pageSize, courseId, sortField, sortOrder));
    }

    @GetMapping
    public Result<PageResult<CourseVO>> getCourseListByT(@RequestParam("pageNum") Integer pageNum,
                                                         @RequestParam("pageSize") Integer pageSize,
                                                         HttpServletRequest request) {
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.TEACHER_ID)));

        return Result.success(courseService.getCourseListByTeacher(pageNum, pageSize, userId));
    }

    @PostMapping
    public Result addCourse(@RequestBody CourseDTO courseDTO,
                            HttpServletRequest request) {
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.TEACHER_ID)));

        courseService.addCourse(userId, courseDTO);
        return Result.success();
    }

    @PutMapping
    public Result updateCourse(@RequestBody CourseUpdateDTO courseUpdateDTO,
                               HttpServletRequest request) {
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.TEACHER_ID)));

        courseService.updateCourse(userId, courseUpdateDTO);
        return Result.success();
    }

    @GetMapping("/subject")
    public Result<PageResult<Subject>> getSubjectList(@RequestParam("pageNum") Integer pageNum,
                                                      @RequestParam("pageSize") Integer pageSize) {
        return Result.success(subjectService.getSubjectList(pageNum, pageSize));
    }

    @DeleteMapping("/expel")
    public Result expel(@RequestParam Long courseId,
                        @RequestParam Long userId){
        courseRecordService.expel(courseId,userId);
        return Result.success();
    }

}
